package cn.hfuu.controller;

import cn.hfuu.domain.SaleVisit;
import cn.hfuu.domain.User;
import cn.hfuu.service.SaleVisitService;
import cn.hfuu.utils.PageBean;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

public class SaleVisitAction extends ActionSupport implements ModelDriven<SaleVisit>{
    private SaleVisit saleVisit = new SaleVisit();
    private SaleVisitService saleVisitService;
    private Integer currentPage;
    private Integer pageSize;


    public String add() throws Exception {
        //得到用户user 与saleVisit表关联
        User user = (User) ActionContext.getContext().getSession().get("user");
        saleVisit.setUser(user);

        //调用service方法
        if(saleVisit.getVisit_id()==null) {
            saleVisitService.save(saleVisit);
        }
        else {
            saleVisitService.update(saleVisit);
        }
        return "list";
    }
    public String toEdit() throws Exception {
        //visit_id
        SaleVisit sv =  saleVisitService.getById(saleVisit.getVisit_id());
        ActionContext.getContext().put("saleVisit",sv);


        return "add";
    }
    public String list() throws Exception {
        //封装离线查询对象
        DetachedCriteria dc = DetachedCriteria.forClass(SaleVisit.class);
        if(saleVisit.getCustomer()!=null && saleVisit.getCustomer().getCust_id()!=null) {
            dc.add(Restrictions.eq("customer.cust_id",saleVisit.getCustomer().getCust_id()));
        }
        //调用service方法进行分页查询
        PageBean pb = saleVisitService.getPageBean(dc,currentPage,pageSize);
        ActionContext.getContext().put("pageBean",pb);
        return "list";
    }


    public SaleVisit getModel() {
        return saleVisit;
    }

    public void setSaleVisitService(SaleVisitService saleVisitService) {
        this.saleVisitService = saleVisitService;
    }

    public Integer getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(Integer currentPage) {
        this.currentPage = currentPage;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }
}
